package com.cloudfast.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.cloudfast.manager.FileStorageManager;
import com.cloudfast.response.FileResponse;

/**
 * 默认的文件处理(支持上传/及word预览)
 *
 * @author liuyw
 */
@RestController
@Api(value = "文件操作接口", tags = {"文件操作接口"})
public class DefaultFileController {

    @Autowired
    private FileStorageManager storageManager;

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping(value = "file/uploadFile")
    @ApiOperation("默认系统上传文件")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "上传的文件流", required = true)})
    public FileResponse uploadFile(MultipartFile file) {
        try {
            return storageManager.upload(file);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 下载
     *
     * @param filePath
     * @return
     */
    @PostMapping(value = "file/download")
    @ApiOperation("下载")
    @ApiImplicitParams({@ApiImplicitParam(name = "filePath", value = "文件路径", required = true)})
    public byte[] download(String filePath) {
        return this.storageManager.download(filePath);
    }

}
